package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwaklT;
import cn.e4j.modular.crm.entity.GwalT;
import cn.e4j.modular.crm.mapper.GwakTMapper;
import cn.e4j.modular.crm.service.GwakTService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  客户资料 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-11
 */
@Service
public class GwakTServiceImpl extends ServiceImpl<GwakTMapper, GwakT> implements GwakTService {

    @Autowired
    private GwalTServiceImpl gwalTService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getHeadList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> headDtos = InvokeUtil.invokeBeanMethod("gwakTMapper,listHeadPage",page, dto);

        return headDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwacList(Dto dto) {
        List<Map<String, Object>> gwacDtos = this.baseMapper.getGwacList(dto);
        return gwacDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwafList(Dto dto) {
        List<Map<String, Object>> gwafDtos = this.baseMapper.getGwafList(dto);
        return gwafDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaeList(Dto dto) {
        List<Map<String, Object>> gwaeDtos = this.baseMapper.getGwaeList(dto);
        return gwaeDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaqList(Dto dto) {
        List<Map<String, Object>> gwaqDtos = this.baseMapper.getGwaqList(dto);
        return gwaqDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwarList(Dto dto) {
        List<Map<String, Object>> gwarDtos = this.baseMapper.getGwarList(dto);
        return gwarDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwagList(Dto dto) {
        List<Map<String, Object>> gwagDtos = this.baseMapper.getGwagList(dto);
        return gwagDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGweaList(Dto dto) {
        List<Map<String, Object>> gweaDtos = this.baseMapper.getGweaList(dto);
        return gweaDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getSysOrgList(Dto dto) {
        List<Map<String, Object>> orgDtos = this.baseMapper.getSysOrgList(dto);
        return orgDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwasList(Dto dto) {
        List<Map<String, Object>> gwasDtos = this.baseMapper.getGwasList(dto);
        return gwasDtos;
    }

    @Transactional(readOnly = true)
    public GwakT getByGwak001(String ent, String gwak001) {
        UpdateWrapper<GwakT> gwakTUpdateWrapper = new UpdateWrapper<>();
        gwakTUpdateWrapper.eq("GWAKENT",ent);
        gwakTUpdateWrapper.eq("GWAK001",gwak001);
        GwakT gwak = this.baseMapper.selectOne(gwakTUpdateWrapper);
        return gwak;
    }

    @Transactional
    @Override
    public void add(GwakT gwakT,GwaklT gwaklT, Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwakT.setGWAKENT(shiroUser.getDBC());
        gwakT.setGWAKUSER(shiroUser.getACCOUNT());
        gwakT.setGWAKGRUP(shiroUser.getORG_ID());
        //gwakT.setGWAKDATE(LocalDate.now());
        gwakT.setGWAKTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        /*gwakT.setGWAKORIG(shiroUser.getORG_ID());
        gwakT.setGWAKORIU(shiroUser.getID());*/
        //gwakT.setGWAK054(gwakT.getGWAK010());
        gwaklT.setGWAKLENT(shiroUser.getDBC());
        gwaklT.setGWAKL001(gwakT.getGWAK001());
        gwaklT.setGWAKL002(LangUtils.getLang(shiroUser.getLANG()));
        //根据省查询商机区域
        inDto.put("gwad001",'4');
        inDto.put("gwad002", gwakT.getGWAK023());
        inDto.put("type", '3');
        List<Map<String, Object>> gwadDto = this.gwalTService.getGwadList(inDto);
        //联系信息有数据，写两笔资料进入客户联系人资料
        String num = gwalTService.getMaxGwal002(shiroUser.getDBC(), gwakT.getGWAK001());
        //管理者有值
        GwalT gwalT1 = new GwalT();
        if(gwakT.getGWAK050() != null || gwakT.getGWAK053() != null){
            if("".equals(num) || num == null){
                num = "1";
            }
            gwalT1.setGWALENT(shiroUser.getDBC());
            gwalT1.setGWAL001(gwakT.getGWAK001());
            gwalT1.setGWAL002(num);
            gwalT1.setGWAL003("N"); //一般地址
            gwalT1.setGWAL010("ASIA"); //洲别
            gwalT1.setGWAL011("CN"); //国别
            gwalT1.setGWAL012(gwadDto.get(0).get("GWAD003").toString()); //区域
            gwalT1.setGWAL013(gwakT.getGWAK023()); //省
            gwalT1.setGWAL014(gwakT.getGWAK024()); //市
            gwalT1.setGWAL015(gwakT.getGWAK025()); //县
            gwalT1.setGWAL016(gwakT.getGWAK026()); //区域
            gwalT1.setGWAL017(gwakT.getGWAK027()); //区域
            gwalT1.setGWAL018(gwakT.getGWAK028()); //区域
            //gwalT1.setGWAL019("");
            gwalT1.setGWAL020(gwakT.getGWAK053()); //电话
            gwalT1.setGWAL021(gwakT.getGWAK050()); //联系人
            gwalT1.setGWAL022("管理者"); //职务
            gwalT1.setGWALORIU(shiroUser.getID());
            gwalT1.setGWALORIG(shiroUser.getORG_ID());
            gwalT1.setGWALDATE(LocalDate.now());
            //gwalT1.setGWAL023(""); //上级领导
            //gwalT1.setGWAL024(""); //生日
            //gwalT1.setGWAL025(""); //兴趣爱好
            //gwalT1.setGWAL026(""); //备注
            gwalT1.setGWALACTI("Y"); //有效否
            gwalT1.insert();

            num = String.valueOf(Integer.parseInt(num)+1);
        }
        //主办人有值
        GwalT gwalT2 = new GwalT();
        if(gwakT.getGWAK031() != null || gwakT.getGWAK030() != null){
            if("".equals(num) || num == null){
                num = "1";
            }
            gwalT2.setGWALENT(shiroUser.getDBC());
            gwalT2.setGWAL001(gwakT.getGWAK001());
            gwalT2.setGWAL002(num);
            gwalT2.setGWAL003("N"); //一般地址
            gwalT2.setGWAL010("ASIA"); //洲别
            gwalT2.setGWAL011("CN"); //国别
            gwalT2.setGWAL012(gwadDto.get(0).get("GWAD003").toString()); //区域
            gwalT2.setGWAL013(gwakT.getGWAK023()); //省
            gwalT2.setGWAL014(gwakT.getGWAK024()); //市
            gwalT2.setGWAL015(gwakT.getGWAK025()); //县
            gwalT2.setGWAL016(gwakT.getGWAK026()); //区域
            gwalT2.setGWAL017(gwakT.getGWAK027()); //区域
            gwalT2.setGWAL018(gwakT.getGWAK028()); //区域
            //gwalT2.setGWAL019("");
            gwalT2.setGWAL020(gwakT.getGWAK030()); //电话
            gwalT2.setGWAL021(gwakT.getGWAK031()); //联系人
            gwalT2.setGWAL022("主办人"); //职务
            gwalT2.setGWALORIU(shiroUser.getID());
            gwalT2.setGWALORIG(shiroUser.getORG_ID());
            gwalT2.setGWALDATE(LocalDate.now());
            //gwalT2.setGWAL023(""); //上级领导
            //gwalT2.setGWAL024(""); //生日
            //gwalT2.setGWAL025(""); //兴趣爱好
            //gwalT2.setGWAL026(""); //备注
            gwalT2.setGWALACTI("Y"); //有效否
            gwalT2.insert();
        }
        gwakT.insert();
        gwaklT.insert();
    }

    @Transactional
    @Override
    public void edit(GwakT gwakT,GwaklT gwaklT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwakT.setGWAKMODU(shiroUser.getACCOUNT());
        //gwakT.setGWAK054(gwakT.getGWAK010());
        UpdateWrapper<GwakT> gwakTTUpdateWrapper = new UpdateWrapper<>();
        gwakTTUpdateWrapper.eq("GWAKENT",gwakT.getGWAKENT());
        gwakTTUpdateWrapper.eq("GWAK001",gwakT.getGWAK001());

        UpdateWrapper<GwaklT> gwaklTUpdateWrapper = new UpdateWrapper<>();
        gwaklTUpdateWrapper.eq("GWAKLENT",gwakT.getGWAKENT());
        gwaklTUpdateWrapper.eq("GWAKL001",gwakT.getGWAK001());

        gwakT.update(gwakTTUpdateWrapper);
        gwaklT.update(gwaklTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(GwakT gwakT,GwaklT gwaklT) {
        //删除客户联系人资料
        GwalT gwalT = new GwalT();
        UpdateWrapper<GwalT> gwalTUpdateWrapper = new UpdateWrapper<>();
        gwalTUpdateWrapper.eq("GWALENT",gwakT.getGWAKENT());
        gwalTUpdateWrapper.eq("GWAL001",gwakT.getGWAK001());
        gwalT.delete(gwalTUpdateWrapper);

        UpdateWrapper<GwakT> gwakTUpdateWrapper = new UpdateWrapper<>();
        gwakTUpdateWrapper.eq("GWAKENT",gwakT.getGWAKENT());
        gwakTUpdateWrapper.eq("GWAK001",gwakT.getGWAK001());

        UpdateWrapper<GwaklT> gwaklTUpdateWrapper = new UpdateWrapper<>();
        gwaklTUpdateWrapper.eq("GWAKLENT",gwakT.getGWAKENT());
        gwaklTUpdateWrapper.eq("GWAKL001",gwakT.getGWAK001());

        gwakT.delete(gwakTUpdateWrapper);
        gwaklT.delete(gwaklTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwakT> gwakTList) {
        for(GwakT gwakT : gwakTList){
            //删除客户联系人资料
            GwalT gwalT = new GwalT();
            UpdateWrapper<GwalT> gwalTUpdateWrapper = new UpdateWrapper<>();
            gwalTUpdateWrapper.eq("GWALENT",gwakT.getGWAKENT());
            gwalTUpdateWrapper.eq("GWAL001",gwakT.getGWAK001());
            gwalT.delete(gwalTUpdateWrapper);

            UpdateWrapper<GwakT> gwakTUpdateWrapper = new UpdateWrapper<>();
            gwakTUpdateWrapper.eq("GWAKENT",gwakT.getGWAKENT());
            gwakTUpdateWrapper.eq("GWAK001",gwakT.getGWAK001());

            GwaklT gwaklT = new GwaklT();
            UpdateWrapper<GwaklT> gwaklTUpdateWrapper = new UpdateWrapper<>();
            gwaklTUpdateWrapper.eq("GWAKLENT",gwakT.getGWAKENT());
            gwaklTUpdateWrapper.eq("GWAKL001",gwakT.getGWAK001());

            gwakT.delete(gwakTUpdateWrapper);
            gwaklT.delete(gwaklTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getERPList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> erpDtos = this.baseMapper.listERP_Page(page, dto);
        return erpDtos;
    }

    @Override
    public int insertERP(Dto dto) {
        int rows = 0;
        try {
            rows = this.baseMapper.insertERP(dto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int updateERP(Dto dto) {
        int rows = 0;
        try {
            rows = this.baseMapper.updateERP(dto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Transactional(readOnly = true)
    public Dto selectByGwak001(Dto dto) {
        Dto outDto = this.baseMapper.selectByGwak001(dto);
        return outDto;

    }

    @Transactional(readOnly = true)
    public Dto getDBinfo(String MB001) {
        Dto DBinfoDtos = this.baseMapper.getDBinfo(MB001);
        return DBinfoDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwakData(Dto dto) {
        List<Dto> list = this.baseMapper.getGwakData(dto);
        Dto gwakDtos = list.size()> 0 ? list.get(0): null;
        return gwakDtos;
    }

    @Transactional
    @Override
    public void updateStatus(List<GwakT> gwakTList) {
        for (GwakT gwakT : gwakTList) {
            UpdateWrapper<GwakT> gwakTUpdateWrapper = new UpdateWrapper<>();
            gwakTUpdateWrapper.eq("GWAKENT", gwakT.getGWAKENT());
            gwakTUpdateWrapper.eq("GWAK001", gwakT.getGWAK001());
            gwakT.update(gwakTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwaa009(Dto dto){
       List<Dto> list = this.baseMapper.getGwaa009(dto);
       return list;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwaa009(Page<Object> page, Dto dto){
       List<Dto> list = this.baseMapper.getGwaa009(page, dto);
       return list;
    }

    @Transactional(readOnly = true)
    public Dto getAddData(Dto dto){
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        outDto.put("GWAKORIG", shiroUser.getORG_ID());
        outDto.put("GWAKORIU", shiroUser.getID());
        outDto.put("GWAKORIU_NAME", shiroUser.getNAME());
        outDto.put("GWAKDATE", DateUtils.formatDate(new Date(), "yyyy-MM-dd"));
        outDto.put("GWAKORIG_NAME", shiroUser.getSysOrgT().getNAME());
        outDto.put("MOBILE", shiroUser.getMOBILE());
        List<Dto> orgList = InvokeUtil.invokeBeanMethod("gwakTMapper,getOrgList",outDto);
        if(orgList != null && orgList.size() > 0) {
            outDto.put("GWAKMLEGAL", orgList.get(0).getString("ID"));
            outDto.put("GWAKMLEGAL_NAME", orgList.get(0).getString("NAME"));
            outDto.put("GWAKPLANT", orgList.get(0).getString("BIZ_CODE"));
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public Dto getCount(Dto inDto, String GWAKL004) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        if("2".equals(inDto.getString("TYPE")) && ToolUtil.isNotEmpty(inDto.getString("GWAK007"))) {
            Dto aDto = inDto;
            aDto.put("GWAKL004", null);
            Dto count1 = this.baseMapper.getCount(aDto);
            if (count1.getBigDecimal("COUNT").compareTo(BigDecimal.ZERO) == 1) { //大于0
                outDto.setAppCode(300);
                return outDto;
            }
        }
        if(ToolUtil.isNotEmpty(GWAKL004)) {
            Dto bDto = inDto;
            bDto.put("GWAK007", null);
            bDto.put("GWAKL004", GWAKL004);
            Dto count2 = this.baseMapper.getCount(bDto);
            if (count2.getBigDecimal("COUNT").compareTo(BigDecimal.ZERO) == 1) { //大于0
                outDto.setAppCode(400);
                return outDto;
            }
        }
        return outDto;
    }

    @Override
    public GwakT getGwak(Dto dto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("GWAKENT", dto.getString("ENT"));
        queryWrapper.eq("GWAK001", dto.getString("GWAK001"));
        GwakT gwakTDtos = this.baseMapper.selectOne(queryWrapper);
        return gwakTDtos;
    }

    @Transactional
    @Override
    public void updateBackInfo(List<GwakT> gwakTList, String GWAK088) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwakT gwakT : gwakTList) {
            gwakT.setGWAK088(shiroUser.getNAME() + ":"+ GWAK088);
            UpdateWrapper<GwakT> gwakTUpdateWrapper = new UpdateWrapper<>();
            gwakTUpdateWrapper.eq("GWAKENT", gwakT.getGWAKENT());
            gwakTUpdateWrapper.eq("GWAK001", gwakT.getGWAK001());
            gwakT.update(gwakTUpdateWrapper);
        }
    }
}
